package com.zygh.lz.controller.pc;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.lz.dao.XareaMapper;
import com.zygh.lz.dao.XareaRecordMapper;
import com.zygh.lz.entity.Xarea;
import com.zygh.lz.entity.XareaRecord;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "区域更改记录")
@RestController
@RequestMapping("/XareaRecor")
public class XareaRecorController {

    @Resource
    private XareaRecordMapper xareaRecordMapper;
    @Resource
    private XareaMapper xareaMapper;


    @ApiOperation(value = "查询所有更改记录")
    @GetMapping("findXareaRecorAll")
    public ResultBean findXareaRecorAll(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<XareaRecord> xareaRecords = xareaRecordMapper.selectList(null);
        return ResultUtil.setOK("success", new PageInfo<>(xareaRecords));
    }


    @ApiOperation(value = "查询更改记录")
    @GetMapping("findXareaRecorByUp")
    public ResultBean findXareaRecorByUp(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> xareaRecorByUp = xareaRecordMapper.findXareaRecorByUp();
        return ResultUtil.setOK("success", new PageInfo<>(xareaRecorByUp));
    }

    @ApiOperation(value = "查询更改前后对比")
    @GetMapping("findByContrast")
    public ResultBean findByContrast(Integer id, Integer type) {

        Map<String, XareaRecord> map = new HashMap<>();
        List<Map<String, XareaRecord>> xareaRecordList = new ArrayList<>();

        if (type != 2) {
            XareaRecord xareaRecord = xareaRecordMapper.selectById(id);
            map.put("before", null);
            map.put("back", xareaRecord);
            xareaRecordList.add(map);
            return ResultUtil.setOK("success", xareaRecordList);
        }


        //修改前  修改记录修改存的是修改前的数据
        XareaRecord xareaRecord = xareaRecordMapper.selectById(id);
        map.put("before", xareaRecord);
        //修改后
        XareaRecord update = xareaRecordMapper.findUpdate(xareaRecord.getXareaId(), id);
        map.put("back", update);
        if (update == null) {
            Xarea xarea = xareaMapper.selectByPrimaryKey(xareaRecord.getXareaId());
            if (xarea != null) {
                XareaRecord xareaRecord1 = new XareaRecord();
                BeanUtils.copyProperties(xarea, xareaRecord1);
                map.put("back", xareaRecord1);
            }

        }
        xareaRecordList.add(map);
        return ResultUtil.setOK("success", xareaRecordList);
    }


    @ApiOperation(value = "根据id查询更改记录")
    @GetMapping("findXareaRecorById")
    public ResultBean findXareaRecorById(Integer id) {
        return ResultUtil.setOK("success", xareaRecordMapper.selectById(id));
    }


    @ApiOperation(value = "根据岗位id查询该岗位所有的修改记录")
    @GetMapping("findXareaRecorByXareaId")
    public ResultBean findXareaRecorByXareaId(Integer id) {
        return ResultUtil.setOK("success", xareaRecordMapper.selectList(Wrappers.<XareaRecord>lambdaQuery()
                .eq(XareaRecord::getXareaId, id)));
    }


}
